package com.inno.subject.domain.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * @author sean
 */
@Slf4j
@Component
public class CacheUtil<V> {

    private final Cache<String, String> LOCAL_CACHE = CacheBuilder
            .newBuilder()
            .maximumSize(5000)
            .concurrencyLevel(Runtime.getRuntime().availableProcessors())
            .expireAfterWrite(10, TimeUnit.MINUTES).build();

    public void clearUp(String cacheKey){
        LOCAL_CACHE.invalidate(cacheKey);
        if (log.isInfoEnabled()){
            log.info("清除缓存：{}", cacheKey);
        }
    }

    public <T> List<V> getSubjectType(String cacheKey, Class<V> clazz, Function<T, List<V>> function) {

        List<V> resultList = new ArrayList<>();
        String content = LOCAL_CACHE.getIfPresent(cacheKey);
        if (StringUtils.isNotBlank(content)) {
            resultList = JSON.parseArray(content, clazz);
        } else {
            resultList = function.apply(null);
            if (CollectionUtil.isNotEmpty(resultList)) {
                LOCAL_CACHE.put(cacheKey, JSON.toJSONString(resultList));
            }
        }

        return resultList;
    }

    public List<V> getCategoryTree(String cacheKey, Class<V> clazz, Function<Long, List<V>> buildFullCategoryTree,
                                   Function<Long, List<V>> buildSubCategoryTree,
                                   Long categoryId) {

        List<V> resultList = new LinkedList<>();
        String content = LOCAL_CACHE.getIfPresent(cacheKey);
        log.info("cacheKey:{}", cacheKey);
        if (StringUtils.isNotBlank(content)) {
            resultList = JSON.parseArray(content, clazz);
        } else {
            if (categoryId == 0) {
                resultList = buildFullCategoryTree.apply(categoryId);
            } else {
                resultList = buildSubCategoryTree.apply(categoryId);
            }
            if (CollectionUtil.isNotEmpty(resultList)) {
                LOCAL_CACHE.put(cacheKey, JSON.toJSONString(resultList));
            }
        }
        return resultList;
    }

    public List<V> get(String cacheKey, Class<V> clazz, Function<V, List<V>> function) {
        String content = LOCAL_CACHE.getIfPresent(cacheKey);
        List<V> resultList = new LinkedList<>();
        if (StringUtils.isNotBlank(content)) {
            resultList = JSON.parseArray(content, clazz);
        } else {
            resultList = function.apply(null);
            if (CollectionUtil.isNotEmpty(resultList)) {
                LOCAL_CACHE.put(cacheKey, JSON.toJSONString(resultList));
            }
        }
        return resultList;
    }
    public List<V> get(String cacheKey, Class<V> clazz) {
        String content = LOCAL_CACHE.getIfPresent(cacheKey);
        List<V> resultList = new LinkedList<>();

        if (StringUtils.isNotBlank(content)) {
            resultList = JSON.parseArray(content, clazz);
        }
        return resultList;
    }
}
